package com.stark.asm.web.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.stark.asm.common.util.action.BaseAction;
import com.stark.asm.common.util.context.LoginContext;
import com.stark.asm.common.util.page.Result;
import com.stark.asm.domain.employee.Employee;
import com.stark.asm.service.employee.EmployeeService;

/**
 * 首页 Action
 * User: YinWei
 * DateTime: 2011-11-29 18:07:32
 * Version：1.0
 */
public class IndexBackgroudAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * Employee code
	 */
	private String employeeCode;
	/**
	 * Employee password
	 */
	private String password;
	/**
	 * Verify code 
	 */
	private String verifyCode;
	
	/**
	 * Employee information service interface
	 */
	private EmployeeService employeeService;

    /**
     * 默认执行的方法，主要用在登录成功之后
     * @return
     * @throws Exception
     */
    public String execute() throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        return SUCCESS;
    }

    /**
     * 左侧菜单展现页
     * @return
     * @throws Exception
     */
    public String frame() throws Exception {
        ActionContext context = ActionContext.getContext();
        ValueStack stack = context.getValueStack();
        LoginContext loginContext = LoginContext.getLoginContext();
        stack.set("loginContext", loginContext);
        return SUCCESS;
    }

    public String toLogin(){
    	return SUCCESS;
    }
    public String login(){
    	
    	if(!StringUtils.isBlank(verifyCode)){
    		if("xumqvr".equalsIgnoreCase(verifyCode)){
    			if(!StringUtils.isBlank(employeeCode)){
    				Result employeeResult=employeeService.getEmployeeByEmployeeCode(employeeCode); //call getEmployeeByEmployeeCode() method
    				if(null!=employeeResult&&employeeResult.getSuccess()){
    					Employee emp=(Employee) employeeResult.get("employee");
    					if(null!=emp){
    						if(!StringUtils.isBlank(password)){
    							if(password.equals(emp.getPassword())){
    								Map<String,Object> map=ActionContext.getContext().getSession();  //get session
    								map.put("employeeCode", employeeCode); //set employeeCode to session
    								map.put("employeeName", emp.getEmployeeName()); //set employeeName  to session
    								
    								return SUCCESS;
    							}else{
    								addActionError("<font color='red'>员工密码不正确</font>");
        	        				return INPUT;
    							}
    							
    						}else{
    							addActionError("<font color='red'>员工密码为空</font>");
    	        				return INPUT;
    						}
    					}else{
    						addActionError("<font color='red'>员工账号不正确</font>");
            				return INPUT;
    					}
    				}else{
    					addActionError("<font color='red'>员工账号不正确</font>");
        				return INPUT;
    				}
    			}else{
    				addActionError("<font color='red'>员工账号为空</font>");
    				return INPUT;
    				
    			}
    			
    		}else{
    			addActionError("<font color='red'>验证码不正确</font>");
           	 	return INPUT;
    		}
    		
    	}else{
    		addActionError("<font color='red'>验证码为空</font>");
          	 return INPUT;
    	}
    }
    /**
     * 退出登录
     *
     * @return
     * @throws Exception
     */
    public String logout() throws Exception {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
      /*  if (logoutCookie != null) {
            for (String s : logoutCookie) {
                deleteCookie(s);
            }
        }*/
        return SUCCESS;
    }

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
    

}